package com.xsmart.goods.dao;

import com.baomidou.mybatisplus.plugins.pagination.Pagination;
import org.apache.ibatis.annotations.*;
import org.omg.PortableInterceptor.INACTIVE;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

@Component
public interface GoodsCategoryDao {
    /**
     * 查询商品分类
     * @param parentId 父类id
     * @return 分类列表
     */
    @Select("<script>SELECT * FROM category WHERE <if test=\"parentId==null\">parent_id=1</if><if test=\"parentId!=null\">parent_id=#{parentId}</if></script>")
    List<Map<String, Object>> queryGoodsCategory(@Param("parentId") Long parentId);

    /**
     * 查找父商品分类
     * @param catId 商品分类id
     * @return
     */
    @Select("<script>SELECT parent_id FROM category WHERE cat_id = #{catId} </script>")
    Map<String, Object> queryPGoodsCategory(@Param("catId") Integer catId);

    /*
    * 通过parent_id查找符合条件的信息id
    * */
    @Select("<script>SELECT cat_id FROM category WHERE parent_id = #{catId} </script>")
    List<Map<String, Object>> queryParentGoodsCategory(@Param("catId") long catId);

    /**
     * 查询商品分类
     * @param catId 分类id
     * @return 分类信息
     */
    @Select("<script>SELECT * FROM category WHERE cat_id=#{catId}</script>")
    Map<String, Object> queryGoodsCategoryInfo(@Param("catId") Long catId);

    /**
     * 分页查询商品分类
     * @param page 分页信息
     * @param goodsCategoryMap 查询参数
     * @return 分类列表
     */
    @Select("<script>" +
            "SELECT * FROM category WHERE " +
            "<if test=\"parentId==null or parentId==''\">parent_id=1</if>" +
            "<if test=\"parentId!=null and parentId!=''\">parent_id=#{parentId}</if>" +
            "<if test=\"cat_name!=null and cat_name!=''\">and cat_name=#{cat_name}</if>" +
            "<if test=\"cat_sts!=null and cat_sts!=''\">and cat_sts=#{cat_sts}</if>" +
            "</script>")
    List<Map<String, Object>> queryGoodsCategoryPage(Pagination page, Map goodsCategoryMap);

    /**查询非顶级分类
     * @param page
     * @param goodsCategoryMap
     * @return
     */
    @Select("<script>" +
            "SELECT * FROM category WHERE " +
            "1=1"+
            "<if test=\"cat_name!=null and cat_name!=''\">and cat_name=#{cat_name}</if>" +
            "<if test=\"cat_sts!=null\">and cat_sts=#{cat_sts}</if>" +
            "</script>")
    List<Map<String, Object>> queryGoodsCategoryPageWithoutParentId(Pagination page, Map goodsCategoryMap);
    /**
     * 删除商品分类
     * @param catId 分类id
     * @return
     */
    @Delete("DELETE FROM category WHERE cat_id=#{catId}")
    int delGoodsCategory(Long catId);

    /**添加商品分类
     * @param goodsCategoryMap
     * @return
     */
    @Insert("INSERT INTO category" +
            "(cat_name," +
            "keywords," +
            "cat_desc," +
            "parent_id," +
            "sort_order," +
            "template_file," +
            "show_in_nav," +
            "is_show," +
            "grade," +
            "filter_attr," +
            "style_icon," +
            "cat_icon," +
            "pinyin_keyword," +
            "cat_alias_name," +
            "touch_icon," +
            "cat_sts)"+
            "VALUES" +
            "(#{goodsCategoryMap.cat_name}," +
            "#{goodsCategoryMap.keywords}," +
            "#{goodsCategoryMap.cat_desc}," +
            "#{goodsCategoryMap.parent_id}," +
            "#{goodsCategoryMap.sort_order}," +
            "#{goodsCategoryMap.template_file}," +
            "#{goodsCategoryMap.show_in_nav}," +
            "#{goodsCategoryMap.is_show}," +
            "#{goodsCategoryMap.grade}," +
            "#{goodsCategoryMap.filter_attr}," +
            "#{goodsCategoryMap.style_icon}," +
            "#{goodsCategoryMap.cat_icon}," +
            "#{goodsCategoryMap.pinyin_keyword}," +
            "#{goodsCategoryMap.cat_alias_name}," +
            "#{goodsCategoryMap.touch_icon},"+
            "#{goodsCategoryMap.cat_sts})")
    int addGoodsCategory(@Param("goodsCategoryMap") Map<String, Object> goodsCategoryMap);

    /**修改分类
     * @param goodsCategoryMap
     * @return
     */
    @Update("update category " +
            "set " +
            "cat_name = #{goodsCategoryMap.cat_name}, " +
            "cat_alias_name= #{goodsCategoryMap.cat_alias_name}," +
            "parent_id = #{goodsCategoryMap.parent_id}," +
            "keywords = #{goodsCategoryMap.keywords}," +
            "sort_order = #{goodsCategoryMap.sort_order}," +
            "cat_sts=#{goodsCategoryMap.cat_sts},"+
            "show_in_nav = #{goodsCategoryMap.show_in_nav}," +
            "is_show = #{goodsCategoryMap.is_show}," +
            "cat_desc = #{goodsCategoryMap.cat_desc}," +
            "grade = #{goodsCategoryMap.grade}," +
            "pinyin_keyword = #{goodsCategoryMap.pinyin_keyword}" +
            "where cat_id = #{goodsCategoryMap.cat_id}")
    int modGoodsType(@Param("goodsCategoryMap")Map goodsCategoryMap);
}
